package Package4;

import java.util.Random;
import java.util.Scanner;

public class red_blue_sys {
    public static void main(String[] args) {
        test8();//双色球彩票系统

    }

    public static void test8() {
        //1.系统生成中奖号码
        int[] keyArr = getNumber();
       /* for (int i = 0; i < keyArr.length; i++) {
            System.out.print(keyArr[i] + " ");
        }*/
        //2.用户输入彩票号码（6 红+ 1 蓝）
        int[] userArr = userInputNumber();

        //3.中奖情况
        int redCount = 0;
        int blueCount = 0;
        //判断红球
        for (int i = 0; i < userArr.length - 1; i++) {
            for (int j = 0; j < keyArr.length - 1; j++) {
                if (userArr[i] == keyArr[j]) {
                    redCount++;
                    break;
                }
            }
        }
        //判断蓝球
        if (userArr[userArr.length - 1] == keyArr[keyArr.length - 1]) {
            blueCount++;
        }
        //判断是否获奖
        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜你，获得一等奖，1000万元");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜你，获得二等奖，500万元");
        } else if ((redCount == 5 && blueCount == 1)) {
            System.out.println("恭喜你，获得三等奖，3000元");
        } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
            System.out.println("恭喜你，获得四等奖，200元");
        } else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)) {
            System.out.println("恭喜你，获得五等奖，10元");
        } else if ((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)) {
            System.out.println("恭喜你，获得六等奖，5元");
        } else {
            System.out.println("谢谢参与！");
        }

        //4.打印球号
        bubbleSort(keyArr);
        bubbleSort(userArr);
        System.out.print("中奖号码：");
        for (int i = 0; i < keyArr.length; i++) {
            System.out.print(keyArr[i] + " ");
        }
        System.out.print("\n用户输入号码： ");
        for (int i = 0; i < userArr.length; i++) {
            System.out.print(userArr[i] + " ");
        }

    }

    public static boolean contains(int[] arr, int prize) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == prize) {
                return true;
            }
        }
        return false;
    }

    public static int[] userInputNumber() {
        int[] arr = new int[7];
        Scanner sc = new Scanner(System.in);
        //输入红球
        for (int i = 0; i < arr.length - 1; ) {
            System.out.println("请输入第" + (i + 1) + "个红球");
            int redNumber = sc.nextInt();
            if (redNumber >= 1 && redNumber <= 33) {
                //1 ~ 33 合法
                boolean flag = contains(arr, redNumber);
                if (!flag) {
                    //不存在，可以添加
                    arr[i] = redNumber;
                    ++i;
                } else {
                    //存在，重新输入
                    System.out.println("该红球号码重复，请重新输入");
                }
            } else {
                //不在指定范围内
                System.out.println("当前红球号码超出范围，请重新输入");
            }
        }
        //输入蓝球
        while (true) {
            System.out.println("请输入蓝球：");
            int blueNumber = sc.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16) {
                arr[arr.length - 1] = blueNumber;
                break;
            } else {
                System.out.println("当前蓝球号码超出范围，请重新输入");
            }
        }
        return arr;
    }

    public static int[] getNumber() {
        int[] arr = new int[7];
        //获得 6 个红球数字
        Random r = new Random();
        for (int i = 0; i < arr.length; ) {
            int redNumber = r.nextInt(33) + 1;
            boolean flag = contains(arr, redNumber);
            if (!flag) {
                arr[i] = redNumber;
                ++i;
            }
        }
        //获得篮球数字
        int blueNumber = r.nextInt(16) + 1;
        arr[arr.length - 1] = blueNumber;
        return arr;
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1 - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
